<?php
 if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class User extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->data['controller'] = 'user';   
        $this->load->model('Muser');
        $this->checkAdmin($this->session->userdata('admin_id'),$this->session->userdata('restaurant_id'));
    }
    
	public function profile(){
		$this->load->library('form_validation');
		$this->form_validation->set_rules('mobile', 'Số điện thoại', 'required');
        $this->form_validation->set_rules('fullname', 'Họ và tên', 'required');
		$this->form_validation->set_rules('pword1', 'Mật khẩu mới', 'matches[pword2]');	
		$this->form_validation->set_rules('pword2', 'Nhập lại mật khẩu mới', '');		
        $this->data['data'] = $this->Muser->getProfile($this->session->userdata('admin_id'));
		
        if($this->form_validation->run() === TRUE)
        {
			$username = $this->getParamString("username");
			$mobile = $this->getParamString("mobile");
			$fullname = $this->getParamString("fullname");
			$pword1 = $this->getParamString("pword1");
			$pword2 = $this->getParamString("pword2");
			if ( !in_array($pword1,array('', null)) && !in_array($pword2,array('', null)) && $pword1 != $pword2 ){
				die('<meta charset="utf-8"><script>alert("Mật khẩu không trùng nhau");window.location.href="'.$this->data['baseurl'].'profile.html";</script>');
			}
            $run = $this->Muser->updateProfileDb($username,$mobile,$fullname,$pword1,$pword2);
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Lưu thành công");window.location.href="'.$this->data['baseurl'].'profile.html";</script>');
            }
        }
        $this->data['action'] = 'profile';
        $this->load->view('template/layout', $this->data);
	}
	
}
